import { message } from 'antd';
import { Effect } from '@/dva/types';
import createImage from '@/service/image/createImage';
import getImageList from '@/service/image/getImageList';

/*
* 创建一个图片
* */
export const createNew: Effect = async (payload, { put }) => {
  put({
    type: 'imageSource/setState',
    payload: { submitLoading: true }
  });
  try {
    await createImage(payload);
    message.success('创建成功！');
  }
  catch (e) {
    message.error(e.message);
  }
  finally {
    put({
      type: 'imageSource/setState',
      payload: { submitLoading: false }
    });
  }
}

/*
* 图片分页
* */
export const fetchImageList: Effect = async (payload, { put }) => {
  const { classify } = payload;
  put({
    type: 'imageSource/setLoading',
    payload: { classify, loading: true }
  });
  try {
    const data = await getImageList(payload);
    put({
      payload: { classify, data },
      type: 'imageSource/splitPage',
    });
  }
  catch (e) {
    message.error(e.message);
  }
  finally {
    put({
      type: 'imageSource/setLoading',
      payload: { classify, loading: false }
    });
  }
}



